package kxfang.com.main.ui;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import kxfang.com.base.activity.MvvmBaseActivity;
import kxfang.com.base.storage.MmkvHelper;
import kxfang.com.base.viewmodel.IMvvmBaseViewModel;

import kxfang.com.common.adapter.ScreenAutoAdapter;
import kxfang.com.common.router.RoutHub;
import kxfang.com.common.router.RouterActivityPath;
import kxfang.com.common.router.RouterFragmentPath;
import kxfang.com.common.utils.HawkUtil;
import kxfang.com.common.utils.StatusBarUtil;
import kxfang.com.main.adapter.MainPageAdapter;

import kxfang.com.main.R;

import kxfang.com.main.databinding.MainActivityMainBinding;
import kxfang.com.main.utils.ColorUtils;

import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import me.majiajie.pagerbottomtabstrip.NavigationController;

/**
 * app 主页面
 *
 * @author darryrzhoong
 */

@Route(path = RoutHub.PAGER_MAIN)
public class MainActivity
        extends MvvmBaseActivity<MainActivityMainBinding, IMvvmBaseViewModel> {

    private List<Fragment> fragments;

    private MainPageAdapter adapter;

    private int[] rbs = {R.id.rb_home, R.id.rb_discover, R.id.rb_msg, R.id.rb_mine};//, R.id.rb_add

    @Override
    protected IMvvmBaseViewModel getViewModel() {
        return null;
    }


    public static void start(Context context) {
        MmkvHelper.getInstance().getMmkv().encode("first", false);
        context.startActivity(new Intent(context, MainActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
//        StatusBarUtil.setTopActivityView(this,viewDataBinding.topView);
        ImmersionBar.with(this)
                .statusBarColor(R.color.main_color_bar)
                .navigationBarColor(R.color.main_color_bar)
                .fitsSystemWindows(true)
                .autoDarkModeEnable(true)
                .init();
        initView();
        initFragment();

    }

    private void initView() {
        viewDataBinding.bottomView.setOnCheckedChangeListener((group, checkedId) -> {
            for (int i = 0; i < rbs.length; i++) {
                if (rbs[i] == checkedId) {
//                    if (i == 2 && HawkUtil.getUserId() == null) {
//                        intent.setClass(this, LoginActivity.class);
//                        startActivity(intent);
//                    } else {
//                        viewDataBinding.cvContentView.setCurrentItem(i);
//                    }

                    viewDataBinding.cvContentView.setCurrentItem(i);
                }
            }
        });
        adapter = new MainPageAdapter(getSupportFragmentManager(),
                FragmentPagerAdapter.BEHAVIOR_SET_USER_VISIBLE_HINT);
        viewDataBinding.cvContentView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    setDrawable(1);
                }else {
                    setDrawable(0);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewDataBinding.cvContentView.setOffscreenPageLimit(4);
        viewDataBinding.cvContentView.setAdapter(adapter);
//        mNavigationController.setupWithViewPager(viewDataBinding.cvContentView);
    }

    private void initFragment() {
        fragments = new ArrayList<>();
        //通过ARouter 获取其他组件提供的fragment
        Fragment homeFragment = (Fragment) ARouter.getInstance().build(RoutHub.PAGER_HOME).navigation(this);
        Fragment findFragment = (Fragment) ARouter.getInstance().build(RoutHub.PAGER_FIND).navigation(this);
        Fragment moreFragment = (Fragment) ARouter.getInstance().build(RoutHub.PAGER_MORE).navigation(this);
        Fragment userFragment = (Fragment) ARouter.getInstance().build(RoutHub.PAGER_USER).navigation(this);
        fragments.add(homeFragment);
        fragments.add(findFragment);
        fragments.add(moreFragment);
        fragments.add(userFragment);
        adapter.setData(fragments);
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.main_activity_main;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    private void setDrawable(int flag){
        int draw = 0;
        String temp = "";
        switch (flag){
            case 0:
                draw = R.drawable.main_home_0;
                temp = "首页";
                break;
            case 1:
                draw = R.drawable.main_home_1;
                temp = "";
                break;
            case 2:
                draw = R.drawable.main_home_2;
                temp = "";
                break;
        }
        viewDataBinding.rbHome.setText(temp);
        Drawable drawable = getResources().getDrawable(draw);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        viewDataBinding.rbHome.setCompoundDrawables(null,drawable,null,null);
    }
}
